<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue 3 Options API vs Composition API</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 20px;
    }
    .container {
      margin-bottom: 20px;
      padding: 20px;
      border: 1px solid #ddd;
      border-radius: 8px;
    }
    button {
      cursor: pointer;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <div id="app">
    <!-- 选项式 API 组件 -->
    <options-counter :initial-value="5"></options-counter>

    <!-- 组合式 API 组件 -->
    <composition-counter :initial-value="5"></composition-counter>
  </div>

  <!-- 引入 Vue 3 -->
  <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
  <script>
    const { createApp, ref, watch, computed, onMounted, onUnmounted } = Vue;

    // 选项式 API 组件
    const OptionsCounter = {
      props: {
        initialValue: {
          type: Number,
          default: 0,
        },
      },
      data() {
        return {
          count: this.initialValue,
        };
      },
      methods: {
        increment() {
          this.count++;
        },
      },
      watch: {
        count(newVal, oldVal) {
          console.log(`选项式 API 计数器: count 从 ${oldVal} 变为 ${newVal}`);
        },
      },
      computed: {
        doubleCount() {
          return this.count * 2;
        },
      },
      created() {
        console.log('选项式 API 组件已创建');
      },
      mounted() {
        console.log('选项式 API 组件已挂载');
      },
      template: `
        <div class="container">
          <h2>选项式 API 计数器</h2>
          <p>Count: {{ count }}</p>
          <p>Double Count: {{ doubleCount }}</p>
          <button @click="increment">Increment</button>
        </div>
      `,
    };

    // 组合式 API 组件
    const CompositionCounter = {
      props: {
        initialValue: {
          type: Number,
          default: 0,
        },
      },
      setup(props) {
        console.log('组合式 API 组件 setup 函数已运行');
        const count = ref(props.initialValue);

        function increment() {
          count.value++;
        }

        // watch
        watch(count, (newVal, oldVal) => {
          console.log(newVal, oldVal)
          console.log(`组合式 API 计数器: count 从 ${oldVal} 变为 ${newVal}`);
        });

        // computed
        const doubleCount = computed(() => count.value * 2);

        // lifecycle hooks
        onMounted(() => {
          console.log('组合式 API 组件已挂载');
        });

        return {
          count,
          doubleCount,
          increment,
        };
      },
      template: `
        <div class="container">
          <h2>组合式 API 计数器</h2>
          <p>Count: {{ count }}</p>
          <p>Double Count: {{ doubleCount }}</p>
          <button @click="increment">Increment</button>
        </div>
      `,
    };

    // 创建 Vue 应用
    createApp({
      components: {
        'options-counter': OptionsCounter,
        'composition-counter': CompositionCounter,
      },
    }).mount('#app');
  </script>
</body>
</html>